package com.fz.controller;

import com.fz.entity.Book;
import com.fz.service.BookServiceImp;
import org.omg.PortableServer.LIFESPAN_POLICY_ID;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/6/30.
 */
@Controller
public class indexController {
    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest req){
        String did =req.getParameter("id");
        String num=req.getParameter("num");

        if(did!=null){
            int nn =Integer.parseInt(did);
            int n = new BookServiceImp().deleteById(nn);

        }
        ModelAndView mv = new ModelAndView("index");
        BookServiceImp bookss  =new BookServiceImp();
        if(num==null){
            int pagesize=3;
            int pagecount=1;
              Map<String ,Integer> map = new HashMap<String,Integer>();
              List<Book> list = bookss.queryAll();
              int mm =list.size();
              int number =(mm%pagesize == 0 ? mm/pagesize:mm/pagesize+1);
              map.put("pagecount",pagecount*pagesize-pagesize);
              map.put("pagesize",pagesize);
                    List<Book> list02 = bookss.page(map);
            mv.addObject("books",list02);
            mv.addObject("num",number);
        }else {
            int p =Integer.parseInt(num);
            int pagesize=3;
            int pagecount=p;
            Map<String ,Integer> map = new HashMap<String,Integer>();
            List<Book> list = bookss.queryAll();
            int mm =list.size();
            int number =(mm%pagesize == 0 ? mm/pagesize:mm/pagesize+1);
            map.put("pagecount",pagecount*pagesize-pagesize);
            map.put("pagesize",pagesize);
            List<Book> list02 = bookss.page(map);
            mv.addObject("books",list02);
            mv.addObject("num",number);
        }
        return mv;
    }

    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(@RequestParam("name") String na,@RequestParam("price") Double don){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name",na);
        map.put("price",don);
        new BookServiceImp().addMap(map);
        //return "redirect:index";
       // System.out.println(na+"***"+don);
        return "forward:index";
    }
    @RequestMapping("/update")
    public ModelAndView update(HttpServletRequest req){
        String id =req.getParameter("id");
        int did =Integer.parseInt(id);
        Book book =new BookServiceImp().queryById(did);
        System.out.println(did);

        ModelAndView mv = new ModelAndView("update");
        mv.addObject("book",book);
        return mv;
    }

    @RequestMapping(value = "/update02",method = RequestMethod.POST)
    public String update02(@RequestParam("name") String na,@RequestParam("price") Double don,@RequestParam("id") Integer id){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("id",id);
        map.put("name",na);
        map.put("price",don);
        new BookServiceImp().updateById(map);
        //return "redirect:index";
        System.out.println(na+"***"+don);
        return "forward:index";

    }
}
